package TwoAgain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopServiceImpl implements IShopService{
    /**购物车集合，以商品编号做键，以购物车对象做值*/
    private static Map<Integer,ShopCommodity> shopMap = new HashMap<>();

    @Override
    public List<ShopCommodity> findAll() {
        return new ArrayList<ShopCommodity>(shopMap.values());
    }

    @Override
    public void buyCommodity(Commodity c) {
        if (shopMap.containsKey(c.getCode())==false){
            ShopCommodity sc = new ShopCommodity(c,1);
            shopMap.put(c.getCode(),sc);
        }else {
            ShopCommodity sc = shopMap.get(c.getCode());
            sc.setNumber(sc.getNum()+1);
        }
    }

    @Override
    public int totalPrice() {
        int total = 0;
        for (ShopCommodity sc:shopMap.values()){
            total += sc.getNum()*sc.getCommodity().getPrice();
        }
        return total;
    }

    @Override
    public void updateNum(int code, int newNum) {
        ShopCommodity sc = shopMap.get(code);
        sc.setNumber(newNum);
    }

    @Override
    public void delCommodity(int code) {
        shopMap.remove(code);
    }


}
